<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :border="true" v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="物品名称" align="center" prop="productName" v-if="columns[0].visible"/>
      <el-table-column label="物品类型" align="center" prop="productType" v-if="columns[1].visible"/>
      <el-table-column label="库存" align="center" prop="quantity" v-if="columns[7].visible">
        <template slot-scope="scope">
          <div style="color: blue"  @click="getSelectProduct(scope.row)">{{scope.row.quantity}}</div>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" v-if="columns[8].visible"/>
      <el-table-column label="单位" align="center" prop="unit" v-if="columns[2].visible"/>
      <el-table-column label="产品图片" align="center" prop="image" width="100" v-if="columns[3].visible">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="80" :height="50" v-if="columns[4].visible"/>
        </template>
      </el-table-column>
      <el-table-column label="产品介绍" align="center" prop="introduce" v-if="columns[5].visible"/>
<!--      <el-table-column label="附件" align="center" prop="enclosure" >-->
<!--        <template slot-scope="scope">-->
<!--          <a :href="'http://10.250.251.120/dev-api'+scope.row.enclosure">文件下载</a>-->
<!--        </template>-->
<!--      </el-table-column>-->

<!--      <el-table-column label="仓库id" align="center" prop="warehouseId" />-->
<!--      <el-table-column label="仓库名称" align="center" prop="warehouseName" v-if="columns[6].visible"/>-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物品名称" prop="productName">
          <el-input style="width:200px" v-model="form.productName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="物品类型" prop="productType">
          <el-select style="width: 200px;" v-model="form.productType" placeholder="请选择物品类型">
            <el-option
              v-for="item in itemTypeList"
              :key="item.id"
              :value="item.dictLabel">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select style="width: 200px;" v-model="form.unit" placeholder="请选择仓库名称">
            <el-option
              v-for="item in unitList"
              :key="item.id"
              :value="item.dictLabel">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品价格" prop="price">
          <el-input style="width:200px" v-model="form.price" placeholder="请输入物品价格" />
        </el-form-item>

<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->

        <el-form-item label="产品介绍" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>

<!--        <el-form-item label="仓库id" prop="warehouseId">-->
<!--          <el-input v-model="form.warehouseId" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="仓库名称" prop="warehouseName">-->
<!--          <el-input v-model="form.warehouseName" type="textarea" placeholder="请输入内容" />-->
<!--          <el-select style="width: 200px;" v-model="form.warehouseName" placeholder="请选择仓库名称">-->
<!--            <el-option-->
<!--              v-for="item in warehouseList"-->
<!--              :key="item.id"-->
<!--              :value="item.warehouseName">-->
<!--            </el-option>-->
<!--          </el-select>-->

<!--        </el-form-item>-->
<!--        <el-form-item label="库存" prop="quantity">-->
<!--          <el-input v-model="form.quantity" placeholder="请输入库存" />-->
<!--          <el-input-number v-model="form.quantity"  :min="1"  label="请输入库存"></el-input-number>-->
<!--        </el-form-item>-->
        <el-form-item label="产品图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
<!--        <el-form-item label="附件" prop="enclosure">-->
<!--          <uplod v-model="form.enclosure"></uplod>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    产品  每个仓库产品数量-->
    <el-dialog title="仓库库存" :visible.sync="quantityOpen" width="500px" append-to-body>
      <el-table :data="quantityList" >
        <el-table-column label="物品名称" align="center" prop="productName" />
        <el-table-column label="物品类型" align="center" prop="productType" />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="仓库" align="center" prop="warehouseName" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {selectProduct, listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/module/product";
import uplod from "@/components/FileUpload/index"
import {listWarehouse} from "@/api/module/warehouse";
import  {getDicts} from '@/api/system/dict/data'

export default {
  name: "Product",
  components:{
    uplod
  },
  data() {
    return {
      quantityOpen:false,
      quantityList:[],
      itemTypeList:[],
      //选择仓库数据
      warehouseList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productType: null,
        model: null,
        unit: null,
        createId: null,
        status: null,
        image: null,
        introduce: null,
        enclosure: null,
        warehouseId: null,
        warehouseName: null,
        quantity: null
      },
      // 表单参数
      form: {},
      unitList:[],//单位
      columns: [
        { key: 0, label: `物品名称`, visible: true },
        { key: 1, label: `物品类型`, visible: true },
        { key: 2, label: `单位`, visible: true },
        { key: 3, label: `产品图片`, visible: true },
        { key: 4, label: `产品介绍`, visible: true },
        { key: 5, label: `附件`, visible: true },
        { key: 6, label: `仓库名称`, visible: true },
        { key: 7, label: `库存`, visible: true },
        { key: 8, label: `价格`, visible: true },

      ],
      // 表单校验
      rules: {
        productName:[
          { required: true, message: '请输入物品名称', trigger: 'blur' },
        ],
        unit:[
          { required: true, message: '请输入单位', trigger: 'blur' },
        ],
        introduce:[
          { required: true, message: '请输入内容', trigger: 'blur' },
        ],
        productType:[
          { required: true, message: '请选择物品类型', trigger: 'blur' },
        ],
       price:[
          { required: true, message: '请输入物品价格', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    this.getList();
    this.WarehouseList()
    this.getUnit()
    this.getItemType()
  },
  methods: {
    getSelectProduct(value){
      console.log(value)
      selectProduct({productId:value.id}).then(res=>{
        console.log(res)
        this.quantityList=res.rows
      })
      this.quantityOpen=true
    },
    /** 查询单位 */
    getUnit(){
      getDicts('unit').then(response=>{
        this.unitList=response.data
      })
    },
    /** 物品类型item_type*/
    getItemType(){
      getDicts('item_type').then(response=>{
        console.log(response)
        this.itemTypeList=response.data
      })
    },

    /** 查询仓库管理列表 */
    WarehouseList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询物品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productType: null,
        model: null,
        unit: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        createId: null,
        remark: null,
        status: null,
        image: null,
        introduce: null,
        enclosure: null,
        warehouseId: null,
        warehouseName: null,
        quantity: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物品管理编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
